<?php
/**
 * Users controller class
 */
class Location_AdminController extends Zend_Controller_Action
{
	public function init()
	{
		$user = new Member_Service_User();

		if(!$user->getIdentity()) {
			if($this->getRequest()->getActionName()!='access-denied')
			$this->_helper->redirector('access-denied');
		}
	}

	public function accessDeniedAction()
	{

	}

	/**
	 * Ajoute un matériel
	 * @return unknown_type
	 */
	public function addMaterielAction()
	{
		$form = new Location_Form_Materiel();


		if($this->getRequest()->isPost()) {
			$postedData = $this->getRequest()->getPost();
			if($form->isValid($postedData)){
				$materielData = array();
				$materielData['title'] = $postedData['title'];
				$materielData['price1'] = $postedData['price1'];
				$materielData['price2'] = $postedData['price2'];
				$materielData['price3'] = $postedData['price3'];
				$materielData['quantity'] = $postedData['quantity'];
				$materielData['processeur'] = $postedData['processeur'];
				$materielData['espace_disque'] = $postedData['espace_disque'];
				$materielData['ram'] = $postedData['ram'];
				$materielData['marque'] = $postedData['marque'];
				$materielData['taille'] = $postedData['taille'];
				$materielData['category_id'] = $postedData['category_id'];
				$materielData['description'] = $postedData['description'];

				$uploadedImages = $this->upload(APPLICATION_PATH . '/../data/location/images','rename');
				foreach($uploadedImages as $name => $src) {
						$materielData[$name] = $src;
				}
				
				//print_r($materielData); exit;
								
				$materiel = new Location_Model_Materiel();
				$materiel->insert($materielData);

				//$this->_helper->redirector('access-denied');
			}
		}


		$this->view->form = $form;
		$this->view->layout()->title = "Ajout d'un matériel";
	}
	
	  /*
	 * Author(s): Lucky Spadille <spadille.lucky@gmail.com>
	 * website: http://my.opera.com/spadille/blog/
	 * @param string directory path, where store file upload
	 * @param string override|rename 
	 * return array An array store file name  
	 * 
	 */
	private function upload($dir_upload,$option = 'override'){
			$upload = new Zend_File_Transfer_Adapter_Http();
    		$upload->setDestination($dir_upload); 
	    	$files = $upload->getFileInfo();
	    	$arrFileName = array();
	    	$countFileUpload = count($files);
	    	if($countFileUpload > 0 ){
		    	$i = 1;
		    	foreach ($files as $file => $info){
		    		if($info['name'] != ''){
			    		$validator = new Zend_Validate_File_Exists($dir_upload);	
			    		if($option == 'rename'){
				    		if($validator->isValid($info['name'])){
				    			
				    			$file_name = $upload->getFileName($info['name']);
				    			preg_match("/\.([^\.]+)$/", $file_name, $matches); 
				    			$file_ext = $matches[1];
				    			if($countFileUpload > 1){
				    				$file_name = time() . '_' . $i . '.' . $file_ext;
				    			}else{
				    				$file_name = time() . '.' . $file_ext;
				    			}
				    			$arrFileName[$file] = $file_name;
				    			$upload->addFilter('Rename', $dir_upload .'/' . $file_name);
				    			$i++;
				    		}
			    		}else{
							$arrFileName[$file] = $info['name'];
						}
		    			$upload->receive($file);
		    		}
		    	}
	    	}
	    	
	    return $arrFileName;
	}
	
	public function indexAction()
	{
		//$liens 
	}
	
	public function addCategoryAction()
	{
		$form = new Location_Form_Category();
		
		if($this->getRequest()->isPost()) {
			$postedData = $this->getRequest()->getPost();
			if($form->isValid($postedData)){
				$category = new Location_Model_Category();
				unset($postedData['id']);
				unset($postedData['submit']);
				$category->insert($postedData);
			}
		}
		
		$this->view->form = $form;
	}
	
}
